package crazy.java.cha08;

import com.alibaba.fastjson.JSON;
import crazy.java.cha09.A;
import org.junit.Test;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.BiFunction;

/**
 * @author futao
 * Created on 2017/12/3 - 10:23.
 */
public class MapClass {
    @Test
    public void testCase001() {
        A a = new A("777");
        Map map = new HashMap();
        map.put("66", "444");
        map.put("66", "444");
        map.put("55", "444");
        map.put("7", a);
        map.put("4", a);
        map.putIfAbsent("hh", "hh");
        map.putIfAbsent("4", "hhh");
        map.get("hh");
        System.out.println(map.getOrDefault("7", "hhhh"));
        map.merge("niu", "niubi", new BiFunction() {
            @Override
            public Object apply(Object o, Object o2) {
                //                return new Map.Entry<o,o2>();
                return o2;
            }
        });
        System.out.println(map.get("7").equals(map.get("4")));
        for (int i = 0; i < 5; i++) {
            System.out.println("=====================");
            map.forEach((k, v) -> System.out.println(k + "===" + v));
        }
    }

    @Test
    public void testCase002() {
        LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap();
        linkedHashMap.put("1", "haha");
        linkedHashMap.put("2", "heihei");
        linkedHashMap.put("3", "hohoho");
        linkedHashMap.entrySet().forEach(it -> System.out.println(it + ";;;"));
        linkedHashMap.forEach((k, v) -> System.out.println(k + "===" + v));
    }

    @Test
    public void testCase003() {
        HashMap<String, Object> first = new HashMap<>();
        HashMap<String, String> second = new HashMap<>();
        HashMap<Integer, String> second2 = new HashMap<>();

        second.put("111", "222");
        second.put("666", "777");

        second2.put(1, "22222");
        second2.put(8, "886");

        first.put("s1", second);
        first.put("s2", second2);

        System.out.println(JSON.toJSONString(first));

    }
}
